/*
 * Copyright (C) 2008-2010 Oliver Horn.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.funcj.tuples;

import java.io.Serializable;
import java.util.Arrays;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import com.google.common.base.Objects;

/**
 * An immutable tuple with four elements.
 *
 * @see Tuple
 *
 * @param <A> the type of the first element
 * @param <B> the type of the second element
 * @param <C> the type of the third element
 * @param <D> the type of the fourth element
 *
 * @author Oliver Horn
 */
@Immutable
public final class Quadruple<A, B, C, D> implements Tuple, Serializable {

	private static final long serialVersionUID = -9208942199808635728L;

	private final A first;
	private final B second;
	private final C third;
	private final D fourth;

	public Quadruple(@Nullable A first, @Nullable B second, @Nullable C third, @Nullable D fourth) {
		this.first = first;
		this.second = second;
		this.third = third;
		this.fourth = fourth;
	}

	/**
	 * Returns the first element of this quadruple.
	 * @return the first element
	 */
	public A first() {
		return first;
	}

	/**
	 * Returns the first element of this quadruple.
	 * @return the first element
	 */
	public B second() {
		return second;
	}

	/**
	 * Returns the third element of this quadruple.
	 * @return the third element
	 */
	public C third() {
		return third;
	}

	/**
	 * Returns the fourth element of this quadruple.
	 * @return the fourth element
	 */
	public D fourth() {
		return fourth;
	}

	/**
	 * {@inheritDoc}
	 * @return always {@code 4}
	 */
	public int arity() {
		return 4;
	}

	/**
	 * {@inheritDoc}
	 */
	public Object[] toArray() {
		return new Object[] { first, second, third, fourth };
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int hashCode() {
		return 923521
			+ (first != null ? 29791 * first.hashCode() : 0)
			+ (second != null ? 961 * second.hashCode() : 0)
			+ (third != null ? 31 * third.hashCode() : 0)
			+ (fourth != null ? fourth.hashCode() : 0);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Quadruple) {
			Quadruple<?, ?, ?, ?> other = (Quadruple<?, ?, ?, ?>) obj;
			return Objects.equal(first, other.first)
				&& Objects.equal(second, other.second)
				&& Objects.equal(third, other.third)
				&& Objects.equal(fourth, other.fourth);
		}
		if (obj instanceof Tuple) {
			Tuple other = (Tuple) obj;
			return (other.arity() == 4) && Arrays.equals(toArray(), other.toArray());
		}
		return false;
	}

	@Override
	public String toString() {
		return "(" + first + ", " + second + ", " + third + ", " + fourth + ")";
	}
}
